package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.domain.dto.LoginDTO;
import com.itheima.domain.ex.CustomException;
import com.itheima.domain.pojo.Emp;
import com.itheima.domain.pojo.EmpExpr;
import com.itheima.domain.vo.LoginVO;
import com.itheima.domain.pojo.PageResult;
import com.itheima.domain.query.EmpQueryParam;
import com.itheima.domain.utils.AliyunOSSOperator;
import com.itheima.domain.utils.JwtUtils;
import com.itheima.mapper.EmpExprMapper;
import com.itheima.mapper.EmpMapper;
import com.itheima.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: Sch
 * @Date: 2025/07/11 15:30
 * @Description:
 */
/*@Service
public class EmpServiceImpl implements EmpService {
    @Autowired
    private EmpMapper empMapper;

    @Override
    public PageResult page(Integer page, Integer pageSize) {
       *//* //1. 获取总记录数
        Long total = empMapper.count();

        //2. 获取结果列表
        Integer start = (page - 1) * pageSize;
        List<Emp> empList = empMapper.list(start, pageSize);

        //3. 封装结果
        return new PageResult(total, empList);*//*

        //1.设置分页参数
        PageHelper.startPage(page,pageSize);

        //2.执行查询
        List<Emp> empList = empMapper.list();
        Page<Emp> p = (Page<Emp>) empList;

        //3.封装结果
        return new PageResult(p.getTotal(),p.getResult());
    }
}*/

/**
 * 员工管理
 */
@Service
public class EmpServiceImpl implements EmpService {

    @Autowired
    private EmpMapper empMapper;
    @Autowired
    private EmpExprMapper empExprMapper;
    @Autowired
    private AliyunOSSOperator aliyunOSSOperator;

    @Override
    public PageResult page(EmpQueryParam empQueryParam) {
        //1. 设置分页参数
        PageHelper.startPage(empQueryParam.getPage(), empQueryParam.getPageSize());
        //2. 执行查询
        List<Emp> empList = empMapper.list(empQueryParam);
        //3. 封装分页结果
        Page<Emp> p = (Page<Emp>) empList;
        return new PageResult(p.getTotal(), p.getResult());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(Emp emp)  {
        emp.setPassword("123456");
       emp.setCreateTime(LocalDateTime.now());
       emp.setUpdateTime(LocalDateTime.now());
        empMapper.insert(emp);


        //3. 保存员工的工作经历信息 - 批量
        Integer empId = emp.getId();
        List<EmpExpr> exprList = emp.getExprList();
        if (!CollectionUtils.isEmpty(exprList)) {
            exprList.forEach(empExpr -> empExpr.setEmpId(empId));
            empExprMapper.insertBatch(exprList);
        }

    }

    /**
     * 查询所有员工
     * @return
     */
    @Override
    public List<Emp>findAll(){
        return empMapper.findAll();
    }

    /**
     * 批量删除员工
     * @param ids
     */
    @Override
    public void deleteByIds(List<Integer> ids) throws Exception {
        List<String> key = empExprMapper.selectKeys(ids);
        //1. 根据Id批量删除员工信息
        empMapper.deleteByIds(ids);
        //2. 根据Id批量删除员工对应的工作经历信息
        empExprMapper.deleteByEmpIds(ids);

        aliyunOSSOperator.deleteByUrls(key);
    }

    @Override
    public Emp getInfo(Integer id) {
        return empMapper.getById(id);
    }

    @Transactional
    @Override
    public void update(Emp emp) {
        emp.setUpdateTime(LocalDateTime.now());
        emp.setCreateTime(LocalDateTime.now());
        empMapper.updateById(emp);
        empExprMapper.deleteByEmpIds(Arrays.asList(emp.getId()));

        Integer empId = emp.getId();
        List<EmpExpr> exprList = emp.getExprList();
        if (!CollectionUtils.isEmpty(exprList)) {
            exprList.forEach(empExpr -> empExpr.setEmpId(empId));
            empExprMapper.insertBatch(exprList);
        }
    }

    @Override
    public LoginVO login(LoginDTO loginDTO) {
        if(loginDTO.getPassword() == null || loginDTO.getUsername() == null){
            throw new CustomException("用户名或密码不能为空");
        }
        Emp empLogin = empMapper.getUsername(loginDTO);
        if (empLogin == null){
            throw new CustomException("用户名或密码不存在");
        }
        if(!empLogin.getPassword().equals(loginDTO.getPassword())){
            throw new CustomException("用户名或密码错误");
        }
        //1. 生成JWT令牌
            Map<String,Object> dataMap = new HashMap<>();
            dataMap.put("id", empLogin.getId());
            dataMap.put("username", empLogin.getUsername());

            String jwt = JwtUtils.generateJwt(dataMap);
            LoginVO loginInfo = new LoginVO(empLogin.getId(), empLogin.getUsername(), empLogin.getName(), jwt);
            return loginInfo;
    }


}
